package com.pinusmusic.web.serlvet;

import com.pinusmusic.domain.Music;
import com.pinusmusic.domain.PageBean;
import com.pinusmusic.domain.ResultInfo;
import com.pinusmusic.domain.Singer;
import com.pinusmusic.service.MusicService;
import com.pinusmusic.service.SingerService;
import com.pinusmusic.service.impl.MusicServiceImpl;
import com.pinusmusic.service.impl.SingerServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

@WebServlet("/singer/*")
public class SingerServlet extends BaseServlet {
    private SingerService singerService = new SingerServiceImpl();
    private MusicService musicService = new MusicServiceImpl();
    public void addSinger(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultInfo info = new ResultInfo();
        Singer addSinger = singerService.getAddSinger(request);
        System.out.println(addSinger);//
        if (addSinger == null) {
            info.setErrorMsg("请检查上传信息！");
        } else {
            boolean flag = singerService.addSinger(addSinger);
            info.setFlag(flag);
            if (flag) {
                info.setErrorMsg("添加成功！");
                System.out.println("添加成功:" + addSinger.getSingername());
                response.sendRedirect(request.getContextPath() + "/adminHtml/singer_list.html");
            } else {
                info.setErrorMsg("添加失败！");
            }
        }
        writeValue(response, info);
    }

    public void deleteSingerAndMusicBySid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //String sid = request.getParameter("sid");
        //System.out.println("servlet:"+sid);
        singerService.singerDelete(request,response);
    }

    public void updateSinger(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultInfo info = singerService.getUpdateSinger(request);
        if (info.isFlag()){
            response.sendRedirect(request.getContextPath()+"/adminHtml/singer_list.html");
        }else {
            request.getRequestDispatcher("/adminHtml/singer_update.html").forward(request,response);
        }
        writeValue(response,info);
    }

    public void findSingerByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //管理员输入的查询
        String inputName = request.getParameter("inputName");
        //获取前端传过来的当前页码
        String curStr = request.getParameter("currentPage");
        //一开始就为第一页
        if (curStr == null || curStr.equals("") || curStr.equals("0")) curStr = "1";
        int currentPage = Integer.parseInt(curStr);
        //设置每页显示记录数
        int rows = 3;
        PageBean<Singer> pb = singerService.findSingerByPage(currentPage, rows, inputName);
        //返回前端
        writeValue(response, pb);
    }

    public void getOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sid = request.getParameter("sid");
        //查询对应的歌手
        System.out.println("sid:"+sid);
        Singer singer = singerService.findSingerBySid(Integer.parseInt(sid));
        request.getSession().setAttribute("singer", singer);
        System.out.println(1);
        writeValue(response, singer);
    }
    //查找歌手的歌曲
    public void findSingerMusicByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String curStr = request.getParameter("currentPage");
//        String sid = request.getParameter("sid");
//        System.out.println("sid:"+sid);
        Singer singer = (Singer) request.getSession().getAttribute("singer");
        if (curStr==null || curStr.equals("") || curStr.equals("0")) curStr="1";
        int currentPage = Integer.parseInt(curStr);
        System.out.println("currentPage:"+currentPage);
        System.out.println("singer:"+singer);
        //设置每次查询条数
        int rows = 4;
        PageBean<Music> pageBean = musicService.findSingerMusicByPage(currentPage, rows, singer.getSid());
        writeValue(response,pageBean);
    }
    //批量删除
    public void deleteMoreSinger(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] sids = request.getParameterValues("sids");
        //System.out.println("获取到的sids：" + Arrays.toString(sids));
        singerService.deleteSingers(sids);
        response.sendRedirect(request.getContextPath() + "/adminHtml/singer_list.html");
    }
    //歌手的模模糊查询
    public void findSingersByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        System.out.println("singer获取的name:"+name);
        //获取前端得到的当前页面
        String curStr = request.getParameter("currentPage");
        //一开始就为第一页
        if (curStr==null || curStr.equals("") || curStr.equals("0")) curStr="1";
        int currentPage = Integer.parseInt(curStr);
        //设置每次查询五条;
        int rows = 5;
        PageBean<Singer> pageBean = singerService.findSingersByName(currentPage, rows, name);
        System.out.println("findSingersByName的结果:"+pageBean);
        writeValue(response,pageBean);
    }
}
